package cn.slipi.common.listener;

import cn.slipi.common.utils.cache.constants.RedisConstants;
import cn.slipi.common.utils.cache.redis.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 监听redis是否连接成功
 *
 * @author lee
 */
@Component
public class RedisStartListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private RedisClient redisClient;


    protected Logger LOG = LoggerFactory.getLogger(RedisStartListener.class);


    public static final String REDIS_TEST_VALUE = "true";

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        try {
            redisClient.set(RedisConstants.REDIS_LISTENER, "true", 60);
            if (REDIS_TEST_VALUE.equals(redisClient.get(RedisConstants.REDIS_LISTENER, String.class))) {
                LOG.info("redis 连接成功!!!");
            } else {
                LOG.error("redis 连接失败...请检查缓存是否开启!!!");
            }
        } catch (Exception e) {
            LOG.error("redis 连接失败...请检查缓存是否开启!!! {}", e.getMessage());
        }
    }
}
